//
//  UIViewController+BaseFramework.m
//  Project
//
//  Created by Suny on 14-11-9.
//  Copyright (c) 2014年 Suny. All rights reserved.
//

#import "UIViewController+BaseFramework.h"

@implementation UIViewController (BaseFramework)


- (NSString *)identifier
{
    return [[NSString alloc] initWithFormat:@"identifier_%p",
                                             self];
}


+ (void)load
{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^
    {
        [self swizzleSelector:@selector(init)
                 withSelector:@selector(initBaseFramework)];

        [self swizzleSelector:@selector(loadView)
                 withSelector:@selector(loadViewBaseFramework)];

    });
}


- (id)initBaseFramework
{
    self = [self initBaseFramework];

    if (self)
    {

    }
    return self;
}

- (void)loadViewBaseFramework
{
    [self loadViewBaseFramework];
    BaseView *view = [[BaseView alloc] initWithFrame:self.view.frame];
    view.transform                  = self.view.transform;
    view.contentScaleFactor         = self.view.contentScaleFactor;
    view.multipleTouchEnabled       = self.view.isMultipleTouchEnabled;
    view.exclusiveTouch             = self.view.isExclusiveTouch;
    view.autoresizingMask           = self.view.autoresizingMask;
    view.clipsToBounds              = self.view.clipsToBounds;
    view.backgroundColor            = self.view.backgroundColor;
    view.alpha                      = self.view.alpha;
    view.opaque                     = self.view.isOpaque;
    view.clearsContextBeforeDrawing = self.view.clearsContextBeforeDrawing;
    view.hidden                     = self.view.isHidden;
    view.contentMode                = self.view.contentMode;
    view.contentStretch             = self.view.contentStretch;
    view.delegate                   = (id <BaseViewDelegate>) self;
    self.view                       = view;
}


#pragma mark BaseViewDelegate

- (void)viewDidAddSubview:(UIView *)view
{
    if (self.hasNavigationBar && self.navigationBar)
    {
        [self.view bringSubviewToFront:self.navigationBar];
    }

}

- (void)viewDidBringSubviewToFront:(UIView *)view
{
    if (self.hasNavigationBar && self.navigationBar)
    {
        [(BaseView *) self.view suppressDelegateForAction:^{
            [self.view bringSubviewToFront:self.navigationBar];
        }];
    }

}


@end
